<template>
  <el-dialog
    title="收款"
    v-model="visible"
    width="65%"
    destroy-on-close
    @closed="$emit('closed')"
  >
    <el-form
      :model="form"
      :inline="true"
      :rules="rules"
      ref="dialogForm"
      label-width="auto"
      label-position="right"
    >
      <el-form-item label="收款方式" prop="repaymentType">
        <el-select
          v-model="form.repaymentType"
          placeholder="请选择收款方式"
          style="width: 100%"
          clearable
        >
          <el-option
            v-for="item in planTypeOptions"
            :key="item.value"
            :label="item.name"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="收款账号" prop="account">
        <el-input
          v-model="form.account"
          clearable
          placeholder="请输入收款账号"
        ></el-input>
      </el-form-item>
      <el-form-item label="收款明细" style="width: 100%">
        <el-table
          ref="table"
          :data="planCollectsList"
          row-key="id"
          stripe
          border
        >
          <el-table-column prop="customerName" label="客户名称" min-width="100">
          </el-table-column>
          <el-table-column prop="planMoney" label="本期应收" width="200">
            <template #default="scope">
              <p>{{ scope.row.planMoney }}元</p>
            </template>
          </el-table-column>
          <el-table-column prop="paymentMoney" label="本期实收" width="200">
            <template #default="scope">
              <span>{{ scope.row.paymentMoney ?? 0 }}元</span>
            </template>
          </el-table-column>
          <el-table-column label="本期欠款" prop="balanceMoney" width="200">
            <template #default="scope">
              <span style="color: green"
                >{{ scope.row.planMoney - scope.row.paymentMoney }}元</span
              >
            </template>
          </el-table-column>
          <!-- <el-table-column label="在审金额" width="200">
            <template #default="scope">
              <span style="color: gold">{{ checkMoney ?? 0 }}元</span>
            </template>
          </el-table-column> -->
          <el-table-column label="单笔收款金额(元)" prop="money" width="250">
            <template #default="scope">
              <el-input-number
                :precision="0"
                v-model.number="scope.row.money"
                placeholder="请输入金额"
                @change="changeMoney($event, scope.row)"
              ></el-input-number>
            </template>
          </el-table-column>
          <!-- <el-table-column prop="num" label="抹零平账" width="200">
            <template #default="scope">
              <el-switch
                v-model="scope.row.balancingAccounts"
                active-text="是"
                inactive-text="否"
                :active-value="1"
                :inactive-value="0"
              >
              </el-switch>
            </template>
          </el-table-column> -->
        </el-table>
      </el-form-item>
      <el-form-item label="抹零平账" prop="account">
        <el-switch
          v-model="form.balancingAccounts"
          active-text="是"
          inactive-text="否"
          :active-value="1"
          :inactive-value="0"
          :disabled="checkMoney > 0"
        >
        </el-switch>
      </el-form-item>
      <el-form-item
        label="备注"
        disabled="true"
        prop="remark"
        style="width: 100%"
      >
        <el-input
          v-model="form.remark"
          clearable
          rows="3"
          type="textarea"
          placeholder="请输入备注内容"
        ></el-input>
      </el-form-item>
      <el-form-item label="附件">
        <sc-upload-file
          v-model="form.otherFileList"
          :limit="10"
          :multiple="false"
          tip="最多上传10个文件,单个文件不要超过20M"
        >
          <el-button type="primary" plain icon="el-icon-upload"
            >上传附件</el-button
          >
        </sc-upload-file>
      </el-form-item>
    </el-form>
    <!-- <el-divider /> -->
    <!-- <check-form-item
      ref="checkFormItem"
      @setCheckConfig="setCheckConfig"
      :field="this.form"
      :checkType="checkType"
    ></check-form-item> -->
    <template #footer>
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" :loading="isSaveing" @click="submit()"
        >保 存</el-button
      >
    </template>
  </el-dialog>
</template>
<script>
import checkFormItem from '@/components/checkProcess/checkFormItem.vue'
import { queryTypeByDic } from '@/utils/getData'

export default {
  components: {
    checkFormItem
  },
  emits: ['success', 'closed'],
  data () {
    return {
      checkType: 5,
      visible: false,
      isSaveing: false,
      selectCustomerLoading: false,
      money: '',
      //表单数据
      form: {
        adminId: '',//跟进人id --登录时id
        billId: '',
        departmentId: '',
        repaymentType: '', //回款方式
        account: "",  //回款账号
        remark: '',   //备注
        otherFileList: [],//附件
        planCollects: [],//收款明细
        paymentType: 3,//收款类型 1：合同收款 2：其他收款 3客户收款
        exempt: '',//是否免审
        balancingAccounts: 0,//是否抹零平账 0否 1是
        workflowJsonList: []
      },
      planCollectsList: [],//计划收款明细列表
      planTypeOptions: [],//回款方式
      checkMoney: 0,//在审金额
      checkForm: {
        userId: '',
      },
      //验证规则
      rules: {
        repaymentType: [
          { required: true, message: '请选择收款方式', trigger: 'change' }
        ]
      }
    }
  },
  mounted () {
    this.getType()
  },

  methods: {

    // 获取回款支付方式
    async getType () {
      this.planTypeOptions = await queryTypeByDic('payment')
    },

    // 审批流配置
    setCheckConfig (list, exempt) {
      this.form.workflowJsonList = list
      this.form.exempt = exempt
    },

    //获取在审金额
    async getBillCheckMoney (id) {
      let { data } = await this.$API.finance.bill.getBillCheckMoney.get({ billId: id })
      this.checkMoney = data
    },

    //显示
    open (row) {
      this.visible = true
      let userInfo = this.$TOOL.data.get('USER_INFO')
      this.form.departmentId = userInfo.departmentId
      this.form.adminId = userInfo.userId
      this.planCollectsList[0] = row
      this.form.billId = row.id
      this.getBillCheckMoney(row.id)

    },

    changeMoney (currentValue, item) {
      if (item.money > item.planMoney - item.paymentMoney) {
        item.money = item.planMoney - item.paymentMoney
      }

    },
    //表单提交方法
    submit () {
      this.$refs.dialogForm.validate(async (valid) => {
        if (valid) {
          let list = []
          let flag = false
          this.planCollectsList.map(item => {
            // if (item.money > item.planMoney - item.paymentMoney) {
            //   this.$message.error("单笔金额不能大于剩余的收款金额")
            //   flag = true
            // }
            if (item.money == null || item.money == 0) {
              this.$message.error(`请填写【${item.customerName}】的单笔收款金额`)
              flag = true
            }
            list.push({
              customerId: item.customerId,
              id: item.id,
              money: item.money,
              planMoney: item.planMoney,
            })
          })
          if (flag) return

          this.form.planCollects = list

          let fileType = ''
          this.form.otherFileList.map(item => {
            if (item.url) {
              let arr = item.url.split('.')
              let type = arr[arr.length - 1]
              // fileType  1图片 2文件
              let imageType = 'jpg/png/jpeg/svg'
              if (imageType.includes(type)) {
                fileType = 1
              } else {
                fileType = 2
              }
              item.fileType = fileType
              item.fileName = item.name
            }
          })
          this.isSaveing = true
          let res = await this.$API.finance.plancollect.updatePlanCollectStatus.post(this.form)
          if (res.code == 200) {
            this.$emit('success')
            this.visible = false
            this.isSaveing = false
            this.$message.success("操作成功")
          }
        }
      })
    },

  }
}
</script>
  
<style lang="scss" scoped>
.formRow {
  :deep(.el-form-item__content) {
    flex-wrap: nowrap;
    .el-button {
      margin-left: 10px;
    }
  }
}
</style>
  